import request from '@/utils/request';

/**
 * Redis API接口
 * @author Logan.Li
 */

// 测试Redis连接
export function ping() {
  return request({
    url: '/redis/ping',
    method: 'get'
  });
}

// 设置缓存
export function setCache(key, value, expire) {
  return request({
    url: '/redis/set',
    method: 'post',
    data: {
      key,
      value,
      expire
    }
  });
}

// 设置字符串缓存 - 避免序列化问题
export function setStringCache(key, value, db = 0) {
  return request({
    url: '/redis/setString',
    method: 'post',
    data: {
      key,
      value,
      db
    }
  });
}

// 获取缓存
export function getCache(key) {
  return request({
    url: `/redis/get/${key}`,
    method: 'get'
  });
}

// 获取字符串缓存 - 避免序列化问题
export function getStringCache(key, db = 0) {
  return request({
    url: '/redis/getString',
    method: 'get',
    params: {
      key,
      db
    }
  });
}

// 删除缓存
export function deleteCache(key, db = 0) {
  return request({
    url: '/redis/delete',
    method: 'delete',
    params: {
      key,
      db
    }
  });
}

// 获取所有缓存键
export function getCacheKeys(pattern = '*', db = 0, page = 1, size = 50) {
  return request({
    url: '/redis/keys',
    method: 'get',
    params: {
      pattern,
      db,
      page,
      size
    }
  });
}

// 获取Redis基础信息
export function getRedisInfo() {
  return request({
    url: '/redis/info',
    method: 'get'
  });
}

// 设置List缓存
export function setListCache(key, items, db = 0) {
  return request({
    url: '/redis/setList',
    method: 'post',
    data: {
      key,
      items,
      db
    }
  });
}

// 设置Set缓存
export function setSetCache(key, members, db = 0) {
  return request({
    url: '/redis/setSet',
    method: 'post',
    data: {
      key,
      members,
      db
    }
  });
}

// 设置Hash缓存
export function setHashCache(key, field, value, db = 0) {
  return request({
    url: '/redis/setHash',
    method: 'post',
    data: {
      key,
      field,
      value,
      db
    }
  });
}

// 设置ZSet缓存
export function setZSetCache(key, member, score, db = 0) {
  return request({
    url: '/redis/setZSet',
    method: 'post',
    data: {
      key,
      member,
      score,
      db
    }
  });
}
